/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.internal.impl.ElementImportImpl;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * The Class AliasSection.
 * 
 * @author Chokri Mraidha
 */
public class AliasSection extends PropertyViewSection {

	/**
	 * The name text.
	 */
	private Text nameText;

	/**
	 * The direct edit focus listener.
	 */
	protected DirectEditFocusListener directEditFocusListener = new DirectEditFocusListener();


	/**
	 * {@inheritDoc}
	 */
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		nameText = getWidgetFactory().createText(composite, ""); //$NON-NLS-1$
		nameText.setEditable(true);
		data = new FormData();
		data.width = 150;
		data.left = new FormAttachment(0, 85);
		data.right = new FormAttachment(100, -100);
		data.top = new FormAttachment(0, 0);
		nameText.setLayoutData(data);

		CLabel nameLabel = getWidgetFactory().createCLabel(composite, "Alias:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(nameText,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(nameText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);
	}

	/**
	 * {@inheritDoc}
	 */
	public void refresh() {
		if(!nameText.isDisposed()) {
			if( getGraphicalElement()!=null){
				nameText.removeFocusListener(directEditFocusListener);
				if( getGraphicalElement().getElementImport().getAlias()!=null){
					nameText.setText(getGraphicalElement().getElementImport().getAlias());
				}
			}
			else{
				nameText.removeFocusListener(directEditFocusListener);
				if( ((ElementImport)getElement()).getAlias()!=null){
					nameText.setText(((ElementImport)getElement()).getAlias());
				}
			}

			nameText.addFocusListener(directEditFocusListener);
		}
	}

	/**
	 * Gets the text.
	 * 
	 * @return the text
	 */
	public String getText() {
		return nameText.getText();
	}

	/**
	 * {@inheritDoc}
	 */
	public void dispose(){
		super.dispose();
		if(nameText!=null && !nameText.isDisposed())
			nameText.removeFocusListener(directEditFocusListener);
	}

	/**
	 * The Class DirectEditFocusListener.
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener {
		// check if the alias has been changed or not
		/**
		 * The label.
		 */
		String label = "";

		/**
		 * {@inheritDoc}
		 */
		public void focusGained(FocusEvent e){
			label = getText().trim();
		}

		/**
		 * {@inheritDoc}
		 */
		public void focusLost(FocusEvent e){
			if(!label.equals(getText().trim())) {
				if( getGraphicalElement()!=null){
						getGraphicalElement().getElementImport().setAlias(nameText.getText().trim());
				}
				else{
					((ElementImport)getElement()).setAlias(nameText.getText().trim());
				}
			}
		}
	};

}
